﻿using MediatR;
using MS.BasicData.Domain.Rabbit;
using MS.BasicData.Infrastructure;
using MS.BasicData.Infrastructure.BaseRepository;
using System.ComponentModel.DataAnnotations;

namespace MS.BasicData.Read.API.Applications.ProductInfos
{
    public class CreateProductInfoRequestHandler : IRequestHandler<CreateProductInfoRequestModel, ApiRestult>
    {
        private readonly IProductInfoBaseRepository productInfoBaseRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="productInfoBaseRepository"></param>
        public CreateProductInfoRequestHandler(IProductInfoBaseRepository productInfoBaseRepository)
        {
            this.productInfoBaseRepository = productInfoBaseRepository;
        }

        public async Task<ApiRestult> Handle(CreateProductInfoRequestModel request, CancellationToken cancellationToken)
        {
            ApiRestult restult = new ApiRestult
            {
                isSuc = true,
                Code = Restult.Success,
                Message = "创建成功"
            };
            try
            {
                ProductInfo info = new ProductInfo();
                info.ProductName = request.ProductName;
                await  productInfoBaseRepository.InsertAsync(info);
            }
            catch (Exception ex)
            {
                restult.isSuc = false;
                restult.Code = Restult.Fail;
                restult.Message = ex.Message;
            }
            return restult;    
        }
    }

    public class CreateProductInfoRequestModel : IRequest<ApiRestult>
    {
        /// <summary>
        /// 产品名称
        /// </summary>
        public string ProductName { get; set; } = string.Empty;
    }

}
